import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/blind_can_transfer_page_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_model.dart';
import 'package:yysk_app/model/user_prop_model/list_can_transfer_page_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:oktoast/oktoast.dart';

class PropBatchGiftController extends GetxController {
  final scrollController = ScrollController();

  final porpList = RxList<ListCanTransferPageItem>();
   final blindList = RxList<BlindCanTransferPageItem>();
  final selectedList = RxList<int>([]);
  int pageNum = 1;
  int pageSize = 20;
  String propId = '';//boxId
  String boxId = '';//boxId
  final drawerCount = 0.obs;
  final num = 0.obs;
  final type = ''.obs;
  final transferType = '3'.obs;
  @override
  void onInit() {
    super.onInit();

    propId = Get.parameters['propId'] ?? (Get.arguments['propId'] ?? '');
     boxId = Get.parameters['boxId'] ?? (Get.arguments['boxId'] ?? '');
    type.value = Get.parameters['type'] ?? (Get.arguments['type'] ?? '1');
     transferType.value = Get.parameters['transferType'] ?? (Get.arguments['transferType'] ?? '3');
     if(transferType.value=='3'){
         getPropList( );
       }else{
         getBlindList();
       }
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
       if(transferType.value=='3'){
         getPropList(loadMore: true);
       }else{
         getBlindList(loadMore: true);
       }
      }
    });
  }

  getPropList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<ListCanTransferPageModel>(
        ProductApi.canTransferPage,
        params: {'pageNum': pageNum, 'pageSize': pageSize, 'propId': propId},
        success: (result) {
          List<ListCanTransferPageItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            porpList.clear();
          }
          porpList.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }
  getBlindList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<BlindCanTransferPageModel>(
        ProductApi.userBoxCanTransferPage,
        params: {'pageNum': pageNum, 'pageSize': pageSize, 'boxId': int.tryParse(boxId)},
        success: (result) {
          List<BlindCanTransferPageItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            blindList.clear();
          }
          blindList.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  selectAll() {
     if(transferType.value=='3'){
            selectedList.value = [];
    var hasLength = 0;
    var maxCount = 19;
    var count = 0;
    for (var element in porpList) {
      if (element.isSelect == true) {
        if (hasLength < 20) {
          hasLength += 1;
        } else {
          showToast('超出最大值20');
          return;
        }
      }
    }
    maxCount = maxCount - hasLength;
    var iterator = porpList.iterator;

    while (iterator.moveNext()) {
      var item = iterator.current;
      if (item.isSelect == false) {
        if (maxCount < count) {
          drawerCount.value++;
          return;
        } else {
          item.isSelect = true;
          count++;
        }
      }
    }
    var chooseNum = 0;
    for (var element in porpList) {
      if (element.isSelect == true) {
        chooseNum++;
      }
    }
    num.value = chooseNum;
    drawerCount.value++;
       }
       else{
           selectedList.value = [];
    var hasLength = 0;
    var maxCount = 19;
    var count = 0;
    for (var element in blindList) {
      if (element.isSelect == true) {
        if (hasLength < 19) {
          hasLength += 1;
        } else {
          showToast('超出最大值20');
          return;
        }
      }
    }
    maxCount = maxCount - hasLength;
    var iterator = blindList.iterator;

    while (iterator.moveNext()) {
      var item = iterator.current;
      if (item.isSelect == false) {
        if (maxCount < count) {
          drawerCount.value++;
          return;
        } else {
          item.isSelect = true;
          count++;
        }
      }
    }
    var chooseNum = 0;
    for (var element in blindList) {
      if (element.isSelect == true) {
        chooseNum++;
      }
    }
    num.value = chooseNum;
    drawerCount.value++;
       }
 
  }

  // getList({loadMore = false}) {}
}
